
//引入网络库
import { get, post } from "../../util/util.js"

// 获取全局应用程序实例对象
var app = getApp();

// 创建页面实例对象
Page({
  /**
   * 页面名称
   */
  name: "minedetail",
  /**
   * 页面的初始数据
   */

  data: {
    title:'',
    uid: '',
    inputType: 0,
    hiddenmodalput: true,
    hiddenmodalputEmail: true,
    contentInputStr: '',
    contentInputStrEmail: '',
    name: '',
    nickname: '',
    phone: '',
    address: '',
    email: '',
    wx: '',
    birth: '',
    sex: '男',
    headerImg: '',
    defaulImg: "http://wolaimage-10029986.image.myqcloud.com/1-2016051421050035278.png",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
  },
  onLoad: function (o) {//user/ModifyUserinfo
    var that = this;
    var sexStr = '';
    post("/user/Userinfo", { uid: o.uid }).then(data => {
      sexStr = data.data.sex;
      if (sexStr == "1") {
        sexStr = '男'
      } else if (sexStr == "2") {
        sexStr = '女'
      }
      that.setData({
        uid: o.uid,
        name: data.data.realname,
        headerImg: data.data.headimg,
        nickname: data.data.nickname,
        phone: data.data.mobile,
        address: data.data.area,
        email: data.data.email,
        wx: data.data.wxaccount,
        birth: data.data.birthday,
        sex: sexStr,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },


  //以下为自定义点击事件 
  /**
  * 监听日期picker选择器
  */
  listenerDatePickerSelected: function (e) {
    var that = this;
    post("/user/ModifyUserinfo", { uid: that.data.uid, birthday: e.detail.value }).then(data => {
      that.setData({
        birth: e.detail.value
      })
    })
  },

  /**
   * 性别
   */
  actionSex: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['男', '女'],
      success: function (res) {
        console.log(res.tapIndex)
        if (res.tapIndex == 0) {
          post("/user/ModifyUserinfo", { uid: that.data.uid, sex: '1' }).then(data => {
            that.setData({
              sex: '男'
            })
          })
        } else if (res.tapIndex == 1) {
          post("/user/ModifyUserinfo", { uid: that.data.uid, sex: '2' }).then(data => {
            that.setData({
              sex: '女'
            })
          })
        }
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  //点击按钮痰喘指定的hiddenmodalput弹出框  
  modalinput: function (e) {
    var that = this;
    var title = e.currentTarget.dataset.title;
    this.setData({
      title: title,
      inputType: e.currentTarget.dataset.type,
      hiddenmodalput: !this.data.hiddenmodalput
    })
  },
  
  //取消按钮  
  cancel: function () {
    var that = this;
    this.setData({
      hiddenmodalput: true
    });
  },

  //确认  
  confirm: function () {
    var that = this;
    if (that.data.contentInputStr != ''){
      if (that.data.inputType == 1) {//姓名
        post("/user/ModifyUserinfo", { uid: that.data.uid, realname: that.data.contentInputStr }).then(data => {
          this.setData({
            hiddenmodalput: true,
            name: that.data.contentInputStr,
            contentInputStr: '',
          })
        })
      } else if (that.data.inputType == 2) {//昵称
        post("/user/ModifyUserinfo", { uid: that.data.uid, nickname: that.data.contentInputStr }).then(data => {
          this.setData({
            hiddenmodalput: true,
            nickname: that.data.contentInputStr,
            contentInputStr: '',
          })
        })
      } else if (that.data.inputType == 3) {//微信
        post("/user/ModifyUserinfo", { uid: that.data.uid, wxaccount: that.data.contentInputStr }).then(data => {
          this.setData({
            hiddenmodalput: true,
            wx: that.data.contentInputStr,
            contentInputStr: '',
          })
        })
      } else if (that.data.inputType == 4) {//常驻
        post("/user/ModifyUserinfo", { uid: that.data.uid, area: that.data.contentInputStr }).then(data => {
          this.setData({
            hiddenmodalput: true,
            address: that.data.contentInputStr,
            contentInputStr: '',
          })
        })
      } else if (that.data.inputType == 5) {//邮箱
        post("/user/ModifyUserinfo", { uid: that.data.uid, email: that.data.contentInputStr }).then(data => {
          this.setData({
            hiddenmodalput: true,
            email: that.data.contentInputStr,
            contentInputStr: '',
          })
        })
      } else if (that.data.inputType == 6) {//手机号
        post("/user/ModifyUserinfo", { uid: that.data.uid, mobile: that.data.contentInputStr }).then(data => {
          this.setData({
            hiddenmodalput: true,
            phone: that.data.contentInputStr,
            contentInputStr: '',
          })
        })
      }
    }else{
      wx.showModal({
        title: '提示',
        content: '输入内容不能为空',
        showCancel: false, //不显示取消按钮
        confirmText: '确定'
      })
    }

  },
   
  //获取用户输入的用户名
  contentInput: function (e) {
    var that = this;
    if (that.data.inputType == 1) {//姓名
      this.setData({
        contentInputStr: e.detail.value
      })
    } else if (that.data.inputType == 2) {//昵称
      this.setData({
        contentInputStr: e.detail.value
      })
    } else if (that.data.inputType == 3) {
      this.setData({
        contentInputStr: e.detail.value
      })
    } else if (that.data.inputType == 4) {//常驻
      this.setData({
        contentInputStr: e.detail.value
      })
    } else if (that.data.inputType == 5) {//邮箱
      this.setData({
        contentInputStr: e.detail.value
      })
    } else if (that.data.inputType == 6) {//手机号
      this.setData({
        contentInputStr: e.detail.value
      })
    }
  },

  /**
   * 邮箱相关=========================================
   */
  cancelEmail: function () {
    var that = this;
    this.setData({
      hiddenmodalputEmail: true
    });
  },

  modalinputEmail: function (e) {
    this.setData({
      hiddenmodalputEmail: !this.data.hiddenmodalputEmail
    })
  },
  confirmEmail: function () {
    var that = this;
    if (that.data.contentInputStrEmail != '') {
      post("/user/ModifyUserinfo", { uid: that.data.uid, email: that.data.contentInputStrEmail }).then(data => {
        this.setData({
          hiddenmodalputEmail: true,
          email: that.data.contentInputStrEmail,
          contentInputStrEmail: '',
        })
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '输入内容不能为空',
        showCancel: false, //不显示取消按钮
        confirmText: '确定'
      })
    }

  },
  contentInputEmail: function (e) {
    var that = this;
    this.setData({
      contentInputStrEmail: e.detail.value
    })
  },



  imgUpLoad: function (event) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9  
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        var tempFilePaths = res.tempFilePaths;
        console.log('图片路径' + tempFilePaths[0]);
        wx.uploadFile({
          url: 'http://119.27.190.136/wola/web/api/image/upload',
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            "Content-Type": "multipart/form-data",
            'accept': 'application/json',
          },
          formData: {
            'file': tempFilePaths[0],
            'uid': 1,
          },
          success: function (res) {
            let json = res.data.trim();
            let arr = JSON.parse(json);
            //图片
            post("/user/ModifyUserinfo", { uid: that.data.uid, headimg: arr.data.url }).then(data => {
              console.log("-ModifyUserinfo====ModifyUserinfo==" + JSON.stringify(data.data));
              wx.showToast({
                title: '头像上传成功',
                icon: 'success',
                duration: 1200
              })
              that.setData({
                headerImg: arr.data.url,
              })
            })
          },
          fail: function (res) {
            console.log('上传失败' + res.data);
            wx.showToast({
              title: '上传失败',
              icon: 'success',
              duration: 1200
            })
          },
        })
      }
    })
  },
  getPhoneNumber: function (e) {
    var that = this;
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    //encryptedData iv uid
    post("/user/updateMobile", { iv: e.detail.iv, uid: that.data.uid, encrypteddata: e.detail.encryptedData }).then(data => {
      console.log('updateMobile返回数据：' + JSON.stringify(data))

      that.setData({
        phone: data.data.mobile
      })
    })
    // wx.login({
    //   success: res => {
    //     console.log(res.code);
    //     post("/user/updateMobile", { iv: e.detail.iv, uid: that.data.uid, encrypteddata: encodeURIComponent(e.detail.encryptedData) }).then(data => {
    //       console.log('updateMobile返回数据：' + JSON.stringify(data)) 
    //     })
    //   }
    // })

  }
})

